﻿using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using System.Collections.Generic;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
namespace WPFExcelReport
{
    public partial class ViewBase
    {

        #region ==IsWFApproved===========
        public static readonly DependencyProperty IsWFApprovedProperty = DependencyProperty.Register("IsWFApproved",
            typeof(bool), typeof(ViewBase),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsWFApprovedChanged));
        public bool IsWFApproved
        {
            get
            {
                return (bool)GetValue(IsWFApprovedProperty);
            }
            set
            {
                SetValue(IsWFApprovedProperty, value);
            }
        }
        private static void OnIsWFApprovedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as ViewBase;
            if (src != null)
            {
                src.IsWFApproved = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==IsWFApplied===========
        public static readonly DependencyProperty IsWFAppliedProperty = DependencyProperty.Register("IsWFApplied",
            typeof(bool), typeof(ViewBase),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsWFAppliedChanged));
        public bool IsWFApplied
        {
            get
            {
                return (bool)GetValue(IsWFAppliedProperty);
            }
            set
            {
                SetValue(IsWFAppliedProperty, value);
            }
        }
        private static void OnIsWFAppliedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as ViewBase;
            if (src != null)
            {
                src.IsWFApplied = (bool)e.NewValue;
                src.OnWFApplied((bool)e.NewValue);
            }
        }
        public virtual void OnWFApplied(bool value)
        {
             
        }
        #endregion
        #region ==IsChkWF===========
        public static readonly DependencyProperty IsChkWFProperty = DependencyProperty.Register("IsChkWF",
            typeof(bool), typeof(ViewBase),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsChkWFChanged));
        public bool IsChkWF
        {
            get
            {
                return (bool)GetValue(IsChkWFProperty);
            }
            set
            {
                SetValue(IsChkWFProperty, value);
            }
        }
        private static void OnIsChkWFChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as ViewBase;
            if (src != null)
            {
                src.IsChkWF = (bool)e.NewValue;
                src.ChkWFChg((bool)e.NewValue);
            }
        }
        public virtual void ChkWFChg(bool value)
        {
            if (!value)
                return;
            if (!IsLoaded)
                return;
            if (KEY == "")
                return;
            IsWFFixed = CHKWF(KEY);
        }
        #endregion
        #region ==IsNewWF===========
        public static readonly DependencyProperty IsNewWFProperty = DependencyProperty.Register("IsNewWF",
            typeof(bool), typeof(ViewBase),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsNewWFChanged));
        public bool IsNewWF
        {
            get
            {
                return (bool)GetValue(IsNewWFProperty);
            }
            set
            {
                SetValue(IsNewWFProperty, value);
            }
        }
        private static void OnIsNewWFChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as ViewBase;
            if (src != null)
            {
                src.IsNewWF = (bool)e.NewValue;
                src.NewWFChg((bool)e.NewValue);
            }
        }
        public virtual void NewWFChg(bool value)
        {
            if (!value)
                return;
            if (!IsLoaded)
                return;
            if (father != null)
            {
                if (father is DocumentBase)
                {
                    ((DocumentBase)father).IsNewWF = value;
                }
            }
        }
        #endregion
        #region ==IsWFFixed===========
        public static readonly DependencyProperty IsWFFixedProperty = DependencyProperty.Register("IsWFFixed",
            typeof(bool), typeof(ViewBase),
            new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsWFFixedChanged));
        public bool IsWFFixed
        {
            get
            {
                return (bool)GetValue(IsWFFixedProperty);
            }
            set
            {
                SetValue(IsWFFixedProperty, value);
            }
        }
        private static void OnIsWFFixedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as ViewBase;
            if (src != null)
            {
                src.IsWFFixed = (bool)e.NewValue;
                src.OWFFixedChg((bool)e.NewValue);
            }
        }
        public virtual void OWFFixedChg(bool value)
        {
            IsCanChg = !value;
            if (!value)
                return;
        }
        #endregion

        #region ==WFNEW===========

        public static readonly DependencyProperty WFNEWProperty = DependencyProperty.Register("WFNEW",
            typeof(Dictionary<string, object>), typeof(ViewBase),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWFNEWChanged));
        public Dictionary<string, object> WFNEW
        {
            get
            {
                return (Dictionary<string, object>)GetValue(WFNEWProperty);
            }
            set
            {
                SetValue(WFNEWProperty, value);
                WFNEWTask = Task.Factory.StartNew(() => OnWFNEWChg(value), new CancellationTokenSource().Token, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
            }
        }

        private static void OnWFNEWChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as ViewBase;
            if (src != null)
            {
                src.WFNEW = (Dictionary<string, object>)e.NewValue;
            }
        }

        public virtual void OnWFNEWChg(Dictionary<string, object> pm)
        {
            if (pm == null)
                return;
            NWFID=VICC_WFEVENTS.SVWF.NEWEVENTS(pm);
        }

        #endregion
        #region ==NWFID===========

        public static readonly DependencyProperty NWFIDProperty = DependencyProperty.Register("NWFID",
            typeof(string), typeof(ViewBase),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnNWFIDChanged));
        public string NWFID
        {
            get
            {
                return (string)GetValue(NWFIDProperty);
            }
            set
            {
                SetValue(NWFIDProperty, value); 
             }
        }

        private static void OnNWFIDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as ViewBase;
            if (src != null)
            {
                src.NWFID = (string)e.NewValue;
                src.OnNWFIDChg((string)e.NewValue);
            }
        }

        public virtual void OnNWFIDChg(string value)
        { 
            if (father != null)
            {
                if (father is DocumentBase)
                {
                    ((DocumentBase)father).NWFID = value;
                }
            }
        }

        #endregion
        #region ==WFORDER===========

        public static readonly DependencyProperty WFORDERProperty = DependencyProperty.Register("WFORDER",
            typeof(string), typeof(ViewBase),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWFORDERChanged));
        public string WFORDER
        {
            get
            {
                return (string)GetValue(WFORDERProperty);
            }
            set
            {
                SetValue(WFORDERProperty, value);
            }
        }

        private static void OnWFORDERChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as ViewBase;
            if (src != null)
            {
                src.WFORDER = (string)e.NewValue;
                src.OnWFORDERChg((string)e.NewValue);
            }
        }

        public virtual void OnWFORDERChg(string value)
        {
            
        }

        #endregion

        
        public bool CHKWF(string key)
        {
            bool IsFixed = false;
            dwData = VICC_WFEVENTS.SVWF.TBMain.NewRow();
            dwData["MANDANT"] = app.Mandant;
            dwData["KRS"] = key.Substring(0, 2);
            dwData["LFD"] = key.Substring(2, 6);
            dwData["POS"] = key.Substring(8, 3);
            dwData["WFEVENTS"] = "MAPP00              ";
            MV.or1 = OracleDB.CreateCommand();
            MV.or1.CommandText = "SELECT P.CONTROL FROM ICC_WFEVENTS P"
                 + " WHERE  P.MANDANT=:MANDANT: AND P.KRS=:KRS: AND P.LFD=:LFD: AND P.POS=:POS: AND WFEVENTS=:WFEVENTS: "
                + " ORDER BY P.ROWNID DESC";
            MV.or1.CommandText = INTEXDBC.InjectData(MV.or1.CommandText, dwData);
            SResult = INTEXDBC.Connect2Proxy(MV.or1);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                if (row["CONTROL"].ToString().Trim() == "NO") IsFixed = true;
            }
            return IsFixed;
        }
    }
}
